package com.atguigu.gulimall.auth.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.atguigu.common.utils.HttpUtils;
import com.atguigu.common.utils.R;
import com.atguigu.common.constant.AuthServerConstant;
import com.atguigu.gulimall.auth.feign.MemberFeignService;
import com.atguigu.gulimall.auth.vo.GiteeSocailUser;
import com.atguigu.gulimall.auth.vo.GiteeToken;
import com.atguigu.common.vo.MemberRespVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Controller
@RequestMapping("/oauth2.0")
public class OAuth2Controller {
    @Autowired
    MemberFeignService memberFeignService;

    @GetMapping("/gitee")
    public String gitee(@RequestParam("code") String code,
                        @RequestParam(value = "error", required = false) String error,
                        @RequestParam(value = "error_description", required = false) String errorDesc,
                        HttpSession session) throws Exception {
        if (StringUtils.isBlank(error)) {
            Map<String, String> body = new HashMap<>();
            body.put("grant_type", "authorization_code");
            body.put("code", code);
            body.put("client_id", "34c4368956389a3a307d232c7545328c2a953853317649dd6e99a5e0d986dde9");
            body.put("client_secret", "3400bd00fc4fb21307f032f7a19359967d3a6e12e2d6bf215ac74570e5b5885c");
            body.put("redirect_uri", "http://auth.gulimall.com/oauth2.0/gitee");
            HttpResponse response = HttpUtils.doPost("https://gitee.com", "/oauth/token", "post", new HashMap<>(), new HashMap<>(), body);

            if (response != null && response.getStatusLine().getStatusCode() == 200) {
                String result = EntityUtils.toString(response.getEntity());
                GiteeToken token = JSON.parseObject(result, GiteeToken.class);
                Map<String, String> querys = new HashMap<>();
                querys.put("access_token", token.getAccess_token());
                HttpResponse userResp = HttpUtils.doGet("https://gitee.com", "/api/v5/user", "get", new HashMap<>(), querys);
                GiteeSocailUser user = JSON.parseObject(EntityUtils.toString(userResp.getEntity()), GiteeSocailUser.class);
                user.setAccessToken(token.getAccess_token());
                user.setExpiresIn(token.getExpires_in());
                user.setSocialUid(String.valueOf(user.getId()));
                user.setSocialType("gitee");

                R r = memberFeignService.socailLogin(user);
                if (r.getCode() == 0) {
                    MemberRespVo member = r.getData("data", new TypeReference<MemberRespVo>() {
                    });
                    session.setAttribute(AuthServerConstant.LOGIN_USER, member);
                    return "redirect:http://gulimall.com";
                } else {
                    return "redirect:http://auth.gulimall.com/login.html";
                }

            } else {
                return "redirect:http://auth.gulimall.com/login.html";
            }
        }

        return "redirect:http://auth.gulimall.com/login.html";
    }
}
